<template>
    <div class="app-container">
        <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch"
                 label-width="68px">
            <el-form-item label="名称" prop="name">
                <el-input
                        v-model="queryParams.name"
                        placeholder="请输入名称"
                        clearable
                        @keyup.enter.native="handleQuery"
                />
            </el-form-item>
            <el-form-item label="描述" prop="descripption">
                <el-input
                        v-model="queryParams.descripption"
                        placeholder="请输入描述"
                        clearable
                        @keyup.enter.native="handleQuery"
                />
            </el-form-item>
            <el-form-item label="原价" prop="originalPrice">
                <el-input
                        v-model="queryParams.originalPrice"
                        placeholder="请输入原价，单位是元"
                        clearable
                        @keyup.enter.native="handleQuery"
                />
            </el-form-item>
            <!-- <el-form-item label="价格" prop="price">
                <el-input
                    v-model="queryParams.price"
                    placeholder="请输入价格，单位是元"
                    clearable
                    @keyup.enter.native="handleQuery"
                />
            </el-form-item> -->
            <el-form-item label="类别" prop="productCategoryName">
                <el-input
                        v-model="queryParams.productCategoryName"
                        placeholder="请输入类别"
                        clearable
                        @keyup.enter.native="handleQuery"
                />
            </el-form-item>
            <el-form-item label="商品主人" prop="username">
                <el-input
                        v-model="queryParams.username"
                        placeholder="请输入商品主人"
                        clearable
                        @keyup.enter.native="handleQuery"
                />
            </el-form-item>
            <el-form-item label="商品审核人" prop="reviewerUserName">
                <el-input
                        v-model="queryParams.reviewerUserName"
                        placeholder="请输入商品审核人"
                        clearable
                        @keyup.enter.native="handleQuery"
                />
            </el-form-item>
            <el-form-item label="成色" prop="fineness">
                <el-input
                        v-model="queryParams.fineness"
                        placeholder="请输入成色 0：全新 1：几乎全新 2：轻微使用痕迹 3：明显使用痕迹 4：外观破损"
                        clearable
                        @keyup.enter.native="handleQuery"
                />
            </el-form-item>
            <!-- <el-form-item label="计量单位" prop="unit">
                <el-input
                    v-model="queryParams.unit"
                    placeholder="请输入计量单位"
                    clearable
                    @keyup.enter.native="handleQuery"
                />
            </el-form-item> -->
            <el-form-item label="模式 " prop="isContribute">
                <el-input
                        v-model="queryParams.isContribute"
                        placeholder="请输入0：售卖 1：捐赠 "
                        clearable
                        @keyup.enter.native="handleQuery"
                />
            </el-form-item>
            <el-form-item label="品牌id" prop="brandId">
                <el-input
                        v-model="queryParams.brandId"
                        placeholder="请输入品牌id"
                        clearable
                        @keyup.enter.native="handleQuery"
                />
            </el-form-item>
            <el-form-item>
                <el-button type="primary" icon="Search" size="small" @click="handleQuery">搜索</el-button>
                <el-button icon="Refresh" size="small" @click="resetQuery">重置</el-button>
            </el-form-item>
        </el-form>

        <el-row :gutter="10" class="mb8">
            <el-col :span="1.5">
                <el-button
                        type="primary"
                        plain
                        icon="Plus"
                        size="small"
                        @click="handleAdd"
                        v-hasPermi="['shm:product:add']"
                >新增
                </el-button>
            </el-col>
            <el-col :span="1.5">
                <el-button
                        type="success"
                        plain
                        icon="Edit"
                        size="small"
                        :disabled="single"
                        @click="handleUpdate"
                        v-hasPermi="['shm:product:edit']"
                >修改
                </el-button>
            </el-col>
            <el-col :span="1.5">
                <el-button
                        type="danger"
                        plain
                        icon="Delete"
                        size="small"
                        :disabled="multiple"
                        @click="handleDelete"
                        v-hasPermi="['shm:product:remove']"
                >删除
                </el-button>
            </el-col>
            <el-col :span="1.5">
                <el-button
                        type="warning"
                        plain
                        icon="Download"
                        size="small"
                        @click="handleExport"
                        v-hasPermi="['shm:product:export']"
                >导出
                </el-button>
            </el-col>
            <right-toolbar :showSearch.sync="showSearch" @queryTable="getVoList"></right-toolbar>
        </el-row>

        <el-table v-loading="loading" :data="productVoList" @selection-change="handleSelectionChange">
            <el-table-column type="selection" width="55" align="center"/>
            <el-table-column label="名称" align="center" prop="name"/>
            <el-table-column label="描述" align="center" prop="description"/>
            <el-table-column label="原价，单位是元" align="center" prop="originalPrice"/>
            <el-table-column label="价格，单位是元" align="center" prop="price"/>
            <el-table-column label="类别" align="center" prop="productCategoryName"/>
            <el-table-column label="商品主人" align="center" prop="username"/>
            <el-table-column label="商品审核人" align="center" prop="reviewerUserName"/>
            <el-table-column label="成色" align="center" prop="fineness"/>
            <el-table-column label="计量单位" align="center" prop="unit"/>
            <el-table-column label="商品状态" align="center" prop="status"/>
            <el-table-column label="模式 " align="center" prop="isContribute"/>
            <el-table-column label="功能状态" align="center" prop="functionalStatus"/>
            <el-table-column label="品牌id" align="center" prop="brandId"/>
            <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
                <template #default="scope">
                    <el-button
                            size="small"
                            type="text"
                            icon="el-icon-edit"
                            @click="handleUpdate(scope.row)"
                            v-hasPermi="['shm:product:edit']"
                    >修改
                    </el-button>
                    <el-button
                            size="small"
                            type="text"
                            icon="el-icon-delete"
                            @click="handleDelete(scope.row)"
                            v-hasPermi="['shm:product:remove']"
                    >删除
                    </el-button>
                </template>
            </el-table-column>
        </el-table>

        <pagination
                v-show="total>0"
                :total="total"
                :page.sync="queryParams.pageNum"
                :limit.sync="queryParams.pageSize"
                @pagination="getVoList"
        />

        <!-- 添加或修改商品对话框 -->
        <!-- <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body> -->
        <el-dialog class="large-dialog" :title="title" v-model="open" width="500px" append-to-body>
            <el-form ref="form" :model="form" :rules="rules" label-width="80px">
                <el-form-item label="名称" prop="name">
                    <el-input v-model="form.name" placeholder="请输入名称"/>
                </el-form-item>
                <el-form-item label="描述" prop="description">
                    <el-input v-model="form.description" placeholder="请输入描述"/>
                </el-form-item>
                <el-form-item label="原价" prop="originalPrice">
                    <el-input v-model="form.originalPrice" placeholder="请输入原价，单位是元"/>
                </el-form-item>
                <el-form-item label="价格" prop="price">
                    <el-input v-model="form.price" placeholder="请输入价格，单位是元"/>
                </el-form-item>
                <el-form-item label="类别" prop="productCategoryName">
                    <el-input v-model="form.productCategoryName" placeholder="请输入类别"/>
                </el-form-item>
                <el-form-item label="商品主人" prop="username">
                    <el-input v-model="form.username" placeholder="请输入商品主人"/>
                </el-form-item>
                <el-form-item label="商品审核人" prop="reviewerUserName">
                    <el-input v-model="form.reviewerUserName" placeholder="请输入商品审核人"/>
                </el-form-item>
                <el-form-item label="成色" prop="fineness">
                    <el-input v-model="form.fineness"
                              placeholder="请输入成色 0：全新 1：几乎全新 2：轻微使用痕迹 3：明显使用痕迹 4：外观破损"/>
                </el-form-item>
                <el-form-item label="计量单位" prop="unit">
                    <el-input v-model="form.unit" placeholder="请输入计量单位"/>
                </el-form-item>
                <el-form-item label="0：售卖 1：捐赠 " prop="isContribute">
                    <el-input v-model="form.isContribute" placeholder="请输入0：售卖 1：捐赠 "/>
                </el-form-item>
                <el-form-item label="品牌id" prop="brandId">
                    <el-input v-model="form.brandId" placeholder="请输入品牌id"/>
                </el-form-item>
            </el-form>
            <div class="parent-container">
                <div slot="footer" class="dialog-footer">
                    <el-button type="primary" @click="submitForm">确 定</el-button>
                    <el-button @click="cancel">取 消</el-button>
                </div>
            </div>
            <!-- <div slot="footer" class="dialog-footer">
                <el-button type="primary" @click="submitForm">确 定</el-button>
                <el-button @click="cancel">取 消</el-button>
            </div> -->

        </el-dialog>
    </div>
</template>

<script>
import {listVoProduct, listProduct, getProduct, delProduct, addProduct, updateProduct} from "@/api/market/product";

export default {
    name: "Product",
    data() {
        return {
            // 遮罩层
            loading: true,
            // 选中数组
            ids: [],
            // 非单个禁用
            single: true,
            // 非多个禁用
            multiple: true,
            // 显示搜索条件
            showSearch: true,
            // 总条数
            total: 0,
            // 商品表格数据
            productList: [],
            productVoList: [],
            // 弹出层标题
            title: "",
            // 是否显示弹出层
            open: false,
            // 查询参数
            queryParams: {
                pageNum: 1,
                pageSize: 10,
                isDeleted: null,
                name: null,
                descripption: null,
                originalPrice: null,
                price: null,
                productCategoryId: null,
                userId: null,
                reviewerId: null,
                fineness: null,
                unit: null,
                status: null,
                isContribute: null,
                functionalStatus: null,
                brandId: null
            },
            // 表单参数
            form: {},
            // 表单校验
            rules: {
                createTime: [
                    {required: true, message: "创建时间不能为空", trigger: "blur"}
                ],
                updateTime: [
                    {required: true, message: "更新时间不能为空", trigger: "blur"}
                ],
                name: [
                    {required: true, message: "名称不能为空", trigger: "blur"}
                ],
                price: [
                    {required: true, message: "价格，单位是元不能为空", trigger: "blur"}
                ],
                productCategoryId: [
                    {required: true, message: "类别不能为空", trigger: "blur"}
                ],
                userId: [
                    {required: true, message: "商品主人不能为空", trigger: "blur"}
                ],
                fineness: [
                    {
                        required: true,
                        message: "成色 0：全新 1：几乎全新 2：轻微使用痕迹 3：明显使用痕迹 4：外观破损不能为空",
                        trigger: "blur"
                    }
                ],
                unit: [
                    {required: true, message: "计量单位不能为空", trigger: "blur"}
                ],
                status: [
                    {required: true, message: "0：在售 1：售出 2：下架 不能为空", trigger: "change"}
                ],
                isContribute: [
                    {required: true, message: "0：售卖 1：捐赠 不能为空", trigger: "blur"}
                ],
                functionalStatus: [
                    {
                        required: true,
                        message: "0：功能完好无维修 1：维修过，可正常使用 2：有小问题，不影响使用 3：无法正常使用不能为空",
                        trigger: "change"
                    }
                ],
                brandId: [
                    {required: true, message: "品牌id不能为空", trigger: "blur"}
                ]
            }
        };
    },
    created() {
        this.getList();
        this.getVoList();
    },
    methods: {
        /** 查询商品列表 */
        getList() {
            this.loading = true;
            listProduct(this.queryParams).then(response => {
                <
                <
                <
                <
                <
                << HEAD
                    this.productList=response.rows;
            ======
                =
                    this.prodctList = response.rows;
            >>>>>>>
                dcf7683e7b0dc828ccc292a1b230fc58c03d2b7c
                this.total = response.total;
                this.loading = false;
            });
        },

        getVoList() {
            this.loading = true;
            listVoProduct(this.queryParams).then(response => {
                this.productVoList = response.rows;
                this.total = response.total;
                this.loading = false;
            });
        },

        // 取消按钮
        cancel() {
            this.open = false;
            this.reset();
        },
        // 表单重置
        reset() {
            this.form = {
                id: null,
                createTime: null,
                updateTime: null,
                isDeleted: null,
                name: null,
                descripption: null,
                originalPrice: null,
                price: null,
                productCategoryId: null,
                userId: null,
                reviewerId: null,
                fineness: null,
                unit: null,
                status: null,
                isContribute: null,
                functionalStatus: null,
                brandId: null
            };
            this.resetForm("form");
        },
        /** 搜索按钮操作 */
        handleQuery() {
            this.queryParams.pageNum = 1;
            this.getVoList();
        },
        /** 重置按钮操作 */
        resetQuery() {
            this.resetForm("queryForm");
            this.handleQuery();
        },
        // 多选框选中数据
        handleSelectionChange(selection) {
            this.ids = selection.map(item => item.id)
            this.single = selection.length !== 1
            this.multiple = !selection.length
        },
        /** 新增按钮操作 */
        handleAdd() {

            console.log(1);
            // this.reset();
            this.open = true;
            this.title = "添加商品";
        },
        /** 修改按钮操作 */
        handleUpdate(row) {
            this.reset();
            const id = row.id || this.ids
            getProduct(id).then(response => {
                this.form = response.data;
                this.open = true;
                this.title = "修改商品";
            });
        },
        /** 提交按钮 */
        submitForm() {
            this.$refs["form"].validate(valid => {
                if (valid) {
                    if (this.form.id != null) {
                        updateProduct(this.form).then(response => {
                            this.$modal.msgSuccess("修改成功");
                            this.open = false;
                            this.getVoList();
                        });
                    } else {
                        addProduct(this.form).then(response => {
                            this.$modal.msgSuccess("新增成功");
                            this.open = false;
                            this.getVoList();
                        });
                    }
                }
            });
        },
        /** 删除按钮操作 */
        handleDelete(row) {
            const ids = row.id || this.ids;
            this.$modal.confirm('是否确认删除商品编号为"' + ids + '"的数据项？').then(function () {
                return delProduct(ids);
            }).then(() => {
                this.getVoList();
                this.$modal.msgSuccess("删除成功");
            }).catch(() => {
            });
        },
        /** 导出按钮操作 */
        handleExport() {
            this.download('shm/product/export', {
                ...this.queryParams
            }, `product_${new Date().getTime()}.xlsx`)
        }
    }
};
</script>
<style>
.large-dialog {
    width: 900px;
    height: 750px;
}

.parent-container {
    display: flex;
    justify-content: center;
    align-content: center;

}
</style>
